﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

namespace ClassicPassWd.domain
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
        {
            _allItems = GenerateDemoItems(snackbarMessageQueue);
            FilterItems(null);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private string _searchKeyword;
        private ObservableCollection<PasswordItem> _allItems;
        private ObservableCollection<PasswordItem> _demoItems;
        private PasswordItem _selectedItem;


        public string SearchKeyword
        {
            get => _searchKeyword;
            set
            {
                _searchKeyword = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PasswordItem)));
                FilterItems(_searchKeyword);
            }
        }

        public ObservableCollection<PasswordItem> PasswordItem
        {
            get => _demoItems;
            set
            {
                _demoItems = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PasswordItem)));
            }
        }

        public PasswordItem SelectedItem
        {
            get => _selectedItem;
            set
            {
                if (value == null || value.Equals(_selectedItem)) return;

                _selectedItem = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItem)));
            }
        }


        private ObservableCollection<PasswordItem> GenerateDemoItems(ISnackbarMessageQueue snackbarMessageQueue)
        {
            if (snackbarMessageQueue == null)
                throw new ArgumentNullException(nameof(snackbarMessageQueue));
            return new ObservableCollection<PasswordItem>
            {
                new PasswordItem("主菜单", new Home()
                ),
                new PasswordItem("简单的重新排列", new ReplacePasswd()
                ),
                new PasswordItem("栅栏密码", new FencePasswd()
                ),
                new PasswordItem("列置换密码", new ColumnExchange()
                ),
                new PasswordItem("Caesar密码", new CaesarPassword()
                ),
                new PasswordItem("Caesar密码的解密",new CaesarPasswordDecode()
                ),
                new PasswordItem("乘法密码", new MultiPassword()
                ),
                new PasswordItem("仿射密码", new AffinePassword()
                ),
                new PasswordItem("维吉尼亚密码", new VirginiaPassword()
                ),
                new PasswordItem("Vernam密码", new VernamPassword()
                ),
            };
        }

        private void FilterItems(string keyword)
        {
            var filteredItems =
                string.IsNullOrWhiteSpace(keyword)
                    ? _allItems
                    : _allItems.Where(i => i.Name.ToLower().Contains(keyword.ToLower()));

            PasswordItem = new ObservableCollection<PasswordItem>(filteredItems);
        }
    }
}